package at.fhv.framework.customizerTools;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 * The MatrixCustomizer class.
 * 
 * @author Michael Sieber
 */
public class MatrixTablePanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 7345036681913220788L;

	private JTable _matrixTable;
	private JTextField	_xField;
	private JTextField	_yField;

	private JButton _changeTableButton;

	private float[]	_tableValues;
	
	public MatrixTablePanel()
	{
		intiGUI();
	}
	
	private void intiGUI()
	{
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(400, 300));
		
		_xField = new JTextField();
		_xField.setPreferredSize(new Dimension(25, 20));
		_yField = new JTextField();
		_yField.setPreferredSize(new Dimension(25, 20));
		
		_changeTableButton = new JButton("change matrix table");
		_changeTableButton.addActionListener(this);

		JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		northPanel.add(new JLabel("rows:"));
		northPanel.add(_xField);
		northPanel.add(new JLabel("columns:"));
		northPanel.add(_yField);
		northPanel.add(_changeTableButton);
		
		JPanel centerPanel = new JPanel(new BorderLayout());

		_matrixTable = new JTable();
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(_matrixTable);
		
		centerPanel.add(scrollPane, BorderLayout.CENTER);
		
		add(northPanel, BorderLayout.NORTH);
		add(centerPanel, BorderLayout.CENTER);
	}

	public float[] translateKernel() 
	{
		return _tableValues;
	}
	
	public void setTableData(float[] data, int rows, int columns)
	{
		_tableValues = data;
		_xField.setText(String.valueOf(rows));
		_yField.setText(String.valueOf(columns));
		
		Float[][] tableData = new Float[rows][columns];
		
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
			{
				tableData[i][j] = data[columns*i+j];
			}
		}
		
		TableModel _matrixTableModel = new DefaultTableModel(tableData, createTableHeader(columns));
		_matrixTable.setModel(_matrixTableModel);
	}

	public void changeTable() 
	{
		int rows = Integer.parseInt(_xField.getText());
		int columns = Integer.parseInt(_yField.getText());

		float[] newData = new float[rows*columns];
		
		for (int row = 0; row < Math.min(columns, _matrixTable.getRowCount()); row++)
		{
			for (int column = 0; column < Math.min(rows, _matrixTable.getColumnCount()); column++)
			{			
				newData[columns*row+column] = Float.parseFloat(_matrixTable.getValueAt(row, column).toString());
			}
		}
		
		setTableData(newData, rows, columns);
	}

	/**
	 * Get the height of the kernel matrix
	 * 
	 * @return
	 */
	public int getKernelHeight() 
	{
		return Integer.parseInt(_xField.getText());
	}

	/**
	 * Get the width of the kernel matrix
	 * 
	 * @return
	 */
	public int getKernelWidth() 
	{
		return Integer.parseInt(_yField.getText());
	}

	private String[] createTableHeader(int columns) {
		String[] tableHeader = new String[columns];
		for (int i = 0; i < columns; i++)
		{
			tableHeader[i] = String.valueOf(i);
		}
		return tableHeader;
	}

	@Override
	public void actionPerformed(ActionEvent evt)
	{
		if (evt.getSource() == _changeTableButton)
		{
			changeTable();
		}
	}
}
